package se.webbzon.boltzmann.game.object.pickup;

import java.util.Collection;

import se.webbzon.boltzmann.collector.Collector;
import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.game.environment.GameEnvironment;
import se.webbzon.boltzmann.game.object.player.AbstractPlayerCharacter;
import se.webbzon.boltzmann.game.region.RegionObject;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldeditor.DeflatableWorldObject;
import se.webbzon.oschi01.worldeditor.WorldObjectDeflation;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.XYPlaneAppearance3D;

public class MathNote extends WorldStaticObject implements RegionObject, DeflatableWorldObject {

	// The name of this deflatable world object
	public static final String worldObjectName = "mathNote";
	
	// The range of this pickup
	private static final double pickupDistance = 16;
	
	// The environment in which this math note is created
	private final AbstractEnvironment env;
	
	// The shadow of this math note
	private final MathNoteShadow shadow;
	
	// A collector used to track all the players in the world
	private Collector<AbstractPlayerCharacter> players;
	
	// The id of this region object
	private long id;
		
	// The maximal level for this math note
	private int maxLevel;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public MathNote(AbstractEnvironment env, long id, int maxLevel) {
		super(	WorldObject.onCreate + WorldObject.onStep + WorldObject.onDestroy, 
				createAppearance());
		this.env = env;
		this.id = id;
		this.maxLevel = maxLevel;
		shadow = new MathNoteShadow(this);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the maximal level for which this math note can be used. **/
	public int getMaxLevel() {
		return maxLevel;
	}

	/** Sets the maximal level for which this math note can be used. **/
	public void setMaxLevel(int maxLevel) {
		this.maxLevel = maxLevel;
	}
	
	@Override public void onCreate(WorldArea world) {
		players = Collector.getCollector(world, AbstractPlayerCharacter.class);
		shadow.launch(world);
	}

	@Override public void onStep(WorldArea world) {
		Collection<AbstractPlayerCharacter> playerCollection = players.getInstances();
		synchronized (playerCollection) {
			for (AbstractPlayerCharacter player : playerCollection) {
				if (distanceTo(player) <= pickupDistance) {
					
					int warning;
					System.out.println("note!");
					
					if (env instanceof GameEnvironment)
						((GameEnvironment) env).addGamePersistence(this);
					
					world.destroyInstance(this);
					break;
				}
			}
		}
	}

	@Override public void onDestroy(WorldArea world) {
		players = null;
		shadow.terminate(world);
	}

	@Override public String getName() {
		return worldObjectName;
	}

	@Override public WorldObjectDeflation getDeflation() {
		return new MathNoteDeflation(0,this);
	}

	@Override public long getId() {
		return id;
	}

	@Override public void setId(long id) {
		this.id = id;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Creates the appearance of this math note. **/
	public static final Appearance3D createAppearance() {
		SimpleAppearance3D appearance = new XYPlaneAppearance3D();
		appearance.setTextureSet(Resources.getTextureSet("pickup_note"));
		return appearance;
	}

}
